/**
 * @(#) NametagProxy.as
 *
 * @date		2013. 08. 07.
 * @version		1.0
 * @author		Park, Jutae
 * @description	명함신청 Proxy - Proxy
 * 				명함신청 리스트를 조회하고 저장, 수정, 삭제한다.
 */
package com.skt.common_hr.nametag.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.nametag.NametagFacade;
	import com.skt.common_hr.nametag.model.vo.NametagVO;
	import com.skt.common_hr.nametag.model.vo.reqNametagVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.formatters.DateFormatter;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class NametagProxy extends CommonProxy
	{
		public static const NAME:String = "Nametag";
		
		public var errCode:int;
		public var errText:String;
		public var url:String;
		public var condoListArr:ArrayCollection;
		public var condoFileArr:ArrayCollection;
		
		public var NametagList:ArrayCollection;
		public var reqNametagList:ArrayCollection;
		public var cbList:ArrayCollection;
		public var readList:ArrayCollection;
		public var updateList:ArrayCollection;
		
		
		public function NametagProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		// @@@@@@@@@@ 명함신청 메뉴 클릭시 데이타그리드에 뿌려지는값 받아노는 RFC통신   @@@@@@@@@@ //
		public function load(pernr:String, sDay:Date, eDay:Date):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_BEGDA"] = DateUtil.toBasicString(sDay);
			params["I_ENDDA"] = DateUtil.toBasicString(eDay);
			
			loadData("Z_HR_N001", params, loadResult, loadFault); 
		}
		
		public var sVisible:Boolean; 
		public function loadResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) { 
				if(event.result.O_SYN == 'X'){
					sVisible = true;
				}else {
					sVisible = false;
				}
				NametagList = parseNametagList(event.result.OTAB);
				facade.sendNotification(NametagFacade.LOAD_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(NametagFacade.LOAD_LIST_FAILED, this);
			}
		}
		
		private function loadFault(event:FaultEvent):void
		{
			facade.sendNotification(NametagFacade.LOAD_LIST_FAILED, this);
		}
		
		private function parseNametagList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:NametagVO = new NametagVO();
				
				vo.PERNR		= rec["PERNR"];
				vo.WSTATUS		= rec["WSTATUS"];
				vo.WSTATX 		= rec["WSTATX"];
				vo.WDATE		= toKorString(rec["WDATE"]);
				vo.SREASN 		= rec["SREASN"];
				vo.SREAST		= rec["SREAST"];
				vo.RTEXT		= rec["RTEXT"];
				vo.SDATE		= toKorString(rec["SDATE"]);
				vo.RQMON		= rec["RQMON"];
				// 변경삭제 유무 
				if(vo.WSTATUS == '1' ||vo.WSTATUS == '4'){
					vo.WSTATUS_F = true;
				} else {
					vo.WSTATUS_F = false;
				}	
				
				retArr.addItem(vo);
			}
			return retArr;
		}
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ //
		
		
		// @@@@@@@@@@    명함신청버튼 클릭시 개인정보 받아오는 RFC통신   @@@@@@@@@@ //
		public function WriteViewLoad(pernr:String):void
		{
			var params:Object = new Object();
			
			params["I_PERNR"]	= pernr;
					
			loadData("Z_HR_N003", params, writeViewListResult, writeViewLisFault); 
		}
		
		public function writeViewListResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) { 
				reqNametagList = parsereqNametagList(event.result.OTAB); 
				facade.sendNotification(NametagFacade.WRITEVIEW_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(NametagFacade.WRITEVIEW_LIST_FAILED, this); 
			}
		}
		
		private function writeViewLisFault(event:FaultEvent):void
		{
			facade.sendNotification(NametagFacade.WRITEVIEW_LIST_FAILED, this);
		}
		
		private static var dateFormatter:DateFormatter = new DateFormatter();
		public static function toKorString(src:String):String
		{
			dateFormatter.formatString = "YYYY-MM-DD";
			return dateFormatter.format(src);
		}
		
		private function parsereqNametagList(srcArr:ArrayCollection):ArrayCollection
		{
			trace("parsereqNametagList");
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:reqNametagVO = new reqNametagVO();
				
				vo.PERNR		= rec["PERNR"]; 			//신청자 사번
				vo.ENAME		= rec["ENAME"]; 			//이름
				vo.ENAME_EN		= rec["ENAME_EN"]; 			//이름(영문)	
				vo.ORGTX		= rec["ORGTX"]; 			//소속
				vo.ORGTX_EN		= rec["ORGTX_EN"]; 			//소속(영문)
				vo.MOVETEL		= rec["MOVETEL"];			//이동전화
				vo.EMAIL		= rec["EMAIL"]; 			//이메일
				vo.OFFADR		= rec["OFFADR"]; 			//회사주소
				vo.OFFADR_EN	= rec["OFFADR_EN"]; 		//회사주소(영문)
				vo.SCHK		 	= rec["SCHK"]; 				//선택옵션체크( 1(직위), 2(직책), 3(본부) )
				vo.JWTXT		= rec["JWTXT"]; 			//직위
				vo.JWTXT_EN		= rec["JWTXT_EN"];  		//직위(영문)
				vo.JCTXT		= rec["JCTXT"]; 			//직책
				vo.JCTXT_EN		= rec["JCTXT_EN"];  		//직책(영문)
				vo.HEAD			= rec["HEAD"]; 				//본부
				vo.HEAD_EN		= rec["HEAD_EN"];  			//본부(영문)
				vo.SREASN		= rec["SREASN"]; 			//신청사유
				vo.SREAST		= rec["SREAST"]; 			//신청사유명
				vo.SDATE		= toKorString(rec["SDATE"]);//신청일
				vo.WSTATUS		= rec["WSTATUS"]; 			//신청상태
				vo.WSTATX		= rec["WSTATX"]; 			//신청상태명
				vo.RTEXT		= rec["RTEXT"]; 			//반려사유
				
				retArr.addItem(vo);
			}
			return retArr;
		}
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ //
		
		// @@@@@@@@@@ 명함신청 write화면에 combobox 값 받아오는 RFC통신   @@@@@@@@@@ //
		public function comboListload(pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			
			loadData("Z_HR_N003", params, loadCbResult, loadCbFault); 
		}
		
		public function loadCbResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {   
				cbList = parseCbList(event.result.OTAB1);
				facade.sendNotification(NametagFacade.LOAD_CBLIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(NametagFacade.LOAD_CBLIST_FAILED, this);
			}
		}
		
		private function loadCbFault(event:FaultEvent):void
		{
			facade.sendNotification(NametagFacade.LOAD_LIST_FAILED, this);
		}
		
		private function parseCbList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			retArr.addItem({label:'신청사유 선택', data:-1});
			for each(var rec:Object in srcArr)
			{
				retArr.addItem({label:rec["TEXT"], data:rec["CODE"]});
			}
			return retArr;
		}
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ //
		
		// @@@@@@@@@@   write화면에서 신청버튼 클릭시 RFC통신   @@@@@@@@@@//
		public function save(pernr:String, rec:reqNametagVO, sMonth:String = null):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			if(sMonth == null){
				params["I_RQMON"] = '';	
			}else {
				params["I_RQMON"] = sMonth;
			}
			
			
			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(rec);
			tables["ITAB"] = table;
			
			saveData("Z_HR_N004", params, tables, insertResult, insertFault);
		}		
		
		public var failedAlert:String; 
		public function insertResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(NametagFacade.SAVE_COMPLETE, this);
			}
			else {
				failedAlert = event.result.RETTEXT;
				facade.sendNotification(NametagFacade.SAVE_FAILED, this);
			}
		}
		
		private function insertFault(event:FaultEvent):void
		{
			facade.sendNotification(NametagFacade.SAVE_FAILED, this);
		}
		
		// @@@@@@@@@@  조회시 클릭시 RFC통신    @@@@@@@@@@//
		public function readLoad(pernr:String, sMonth:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_RQMON"] = sMonth;
			
			loadData("Z_HR_N003", params, readResult, readFault);
			
		}

		public function readResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				readList = parseReadList(event.result.OTAB);
				facade.sendNotification(NametagFacade.READ_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(NametagFacade.READ_LIST_FAILED, this);
			}
		}
		
		private function readFault(event:FaultEvent):void
		{
			facade.sendNotification(NametagFacade.READ_LIST_FAILED, this);
		}
		
		private function parseReadList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:reqNametagVO = new reqNametagVO();
				
				vo.PERNR		= rec["PERNR"]; 	//신청자 사번
				vo.ENAME		= rec["ENAME"]; 	//이름
				vo.ENAME_EN		= rec["ENAME_EN"]; 	//이름(영문)	
				vo.ORGTX		= rec["ORGTX"]; 	//소속
				vo.ORGTX_EN		= rec["ORGTX_EN"];  //소속(영문)
				vo.MOVETEL		= rec["MOVETEL"];	 //이동전화
				vo.EMAIL		= rec["EMAIL"]; 	//이메일
				vo.OFFADR		= rec["OFFADR"]; 	//회사주소
				vo.OFFADR_EN	= rec["OFFADR_EN"]; //회사주소(영문)
				vo.SCHK		 	= rec["SCHK"]; 		//선택옵션체크( 1(직위), 2(직책), 3(본부) )
				vo.JWTXT		= rec["JWTXT"]; 	//직위
				vo.JWTXT_EN		= rec["JWTXT_EN"];  //직위(영문)
				vo.JCTXT		= rec["JCTXT"]; 	//직책
				vo.JCTXT_EN		= rec["JCTXT_EN"];  //직책(영문)
				vo.HEAD			= rec["HEAD"]; 		//본부
				vo.HEAD_EN		= rec["HEAD_EN"];   //본부(영문)
				vo.SREASN		= rec["SREASN"]; 	//신청사유
				vo.SREAST		= rec["SREAST"]; 	//신청사유명
				vo.SDATE		= toKorString(rec["SDATE"]); 	//신청일
				vo.WSTATUS		= rec["WSTATUS"]; 	//신청상태
				vo.WSTATX		= rec["WSTATX"]; 	//신청상태명
				vo.RTEXT		= rec["RTEXT"]; 	//반려사유
				
				retArr.addItem(vo);
			}
			return retArr;
		}
		
		// @@@@@@@@@@  변경버튼 클릭시 RFC통신   @@@@@@@@@@//
		public function update(pernr:String, month:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_RQMON"] = month; 
			
			loadData("Z_HR_N003", params, updateResult, updateFault);
		}
		
		public function updateResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {   
				updateList = parseUpdatedList(event.result.OTAB);
				facade.sendNotification(NametagFacade.UPDATE_COMPLETE, this);
			}
			else {
				facade.sendNotification(NametagFacade.UPDATE_FAILED, this);
			}
		}
		
		private function parseUpdatedList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:reqNametagVO = new reqNametagVO();
				
				vo.PERNR		= rec["PERNR"]; 	//신청자 사번
				vo.ENAME		= rec["ENAME"]; 	//이름
				vo.ENAME_EN		= rec["ENAME_EN"]; 	//이름(영문)	
				vo.ORGTX		= rec["ORGTX"]; 	//소속
				vo.ORGTX_EN		= rec["ORGTX_EN"];  //소속(영문)
				vo.MOVETEL		= rec["MOVETEL"];	 //이동전화
				vo.EMAIL		= rec["EMAIL"]; 	//이메일
				vo.OFFADR		= rec["OFFADR"]; 	//회사주소
				vo.OFFADR_EN	= rec["OFFADR_EN"]; //회사주소(영문)
				vo.SCHK		 	= rec["SCHK"]; 		//선택옵션체크( 1(직위), 2(직책), 3(본부) )
				vo.JWTXT		= rec["JWTXT"]; 	//직위
				vo.JWTXT_EN		= rec["JWTXT_EN"];  //직위(영문)
				vo.JCTXT		= rec["JCTXT"]; 	//직책
				vo.JCTXT_EN		= rec["JCTXT_EN"];  //직책(영문)
				vo.HEAD			= rec["HEAD"]; 		//본부
				vo.HEAD_EN		= rec["HEAD_EN"];   //본부(영문)
				vo.SREASN		= rec["SREASN"]; 	//신청사유
				vo.SREAST		= rec["SREAST"]; 	//신청사유명
				vo.SDATE		= toKorString(rec["SDATE"]); 	//신청일
				vo.WSTATUS		= rec["WSTATUS"]; 	//신청상태
				vo.WSTATX		= rec["WSTATX"]; 	//신청상태명
				vo.RTEXT		= rec["RTEXT"]; 	//반려사유
				
				retArr.addItem(vo);
			}
			return retArr;
		}

		private function updateFault(event:FaultEvent):void
		{
			facade.sendNotification(NametagFacade.UPDATE_FAILED, this);
		}
		
		// @@@@@@@@@@  삭제버튼  클릭시 RFC통신   @@@@@@@@@@//
		public function del(pernr:String, month:String):void
		{ 
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_RQMON"] = month; 			
			

			loadData("Z_HR_N002", params, delResult, delFault); 
		}
		
		public function delResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(NametagFacade.DELETE_COMPLETE, this);
			}
			else {
				facade.sendNotification(NametagFacade.DELETE_FAILED, this);
			}
		}
		
		private function delFault(event:FaultEvent):void
		{
			facade.sendNotification(NametagFacade.DELETE_FAILED, this);
		}
		
	}
}